package com.itbaizhan.openhis.controller;

import com.itbaizhan.openhis.dto.NoticeDto;
import com.itbaizhan.openhis.service.NoticeService;
import com.itbaizhan.openhis.utils.ShiroSecurityUtils;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 通知公告控制器
 */
@RestController
@RequestMapping("system/notice")
@Slf4j
public class NoticeController {
	@Autowired
	private NoticeService noticeService;
	
	/**
	 * 查询所有通知公告
	 * @param noticeDto
	 * @return
	 */
	@GetMapping("listNoticeForPage")
	public AjaxResult listNoticeForPage(NoticeDto noticeDto){
		log.info("查询参数：{}",noticeDto);
		DataGridView dataGridView = noticeService.listNoticeForPage(noticeDto);
		return AjaxResult.success("查询成功",dataGridView.getData(), dataGridView.getTotal());
	}
	
	/**
	 * 添加通知公告
	 * @param noticeDto
	 * @return
	 */
	@PostMapping("addNotice")
	public AjaxResult addNotice(@Validated NoticeDto noticeDto){
		log.info("添加参数：{}",noticeDto);
		noticeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
		return AjaxResult.toAjax(noticeService.addNotice(noticeDto));
	}
	
	/**
	 * 修改通知公告
	 * @param noticeDto
	 * @return
	 */
	@PostMapping("updateNotice")
	public AjaxResult updateNotice(@Validated NoticeDto noticeDto){
		log.info("修改参数：{}",noticeDto);
		noticeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
		return AjaxResult.toAjax(noticeService.updateNotice(noticeDto));
	}
	
	/**
	 * 删除通知公告
	 * @param noticeIds
	 * @return
	 */
	@DeleteMapping("deleteNoticeByIds/{noticeIds}")
	public AjaxResult deleteNoticeByIds(@PathVariable @Validated
											@NotEmpty(message = "字典ID不能为空") Long [] noticeIds){
		log.info("删除参数：{}",noticeIds);
		return AjaxResult.toAjax(noticeService.deleteNoticeByIds(noticeIds));
	}
	
	/**
	 * 根据id查询通知公告
	 * @param noticeId
	 * @return
	 */
	@GetMapping("getNoticeById/{noticeId}")
	public AjaxResult getNoticeById(@PathVariable @Validated
									@NotNull(message = "通知ID不能为空")
									Long noticeId){
		return AjaxResult.success(noticeService.getOne(noticeId));
	}
}
